<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/config.inc.php');
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/top.inc.php');

$section_id = mysql_real_escape_string(strip_tags($_POST['sec_id']));
$csm_id = mysql_real_escape_string(strip_tags($_POST['csm_id']));
$course_id = mysql_real_escape_string(strip_tags($_POST['cid']));
$subject_id = mysql_real_escape_string(strip_tags($_POST['sid']));
$cl_csm_id = mysql_real_escape_string(strip_tags($_POST['id']));

$get_subject_data = mysql_query("SELECT * FROM course_subjects WHERE id = $subject_id");
$subject_name = mysql_result($get_subject_data,0,'subject_title');
$abbreviation = mysql_result($get_subject_data,0,'abbreviation');

$get_course_data = mysql_query("SELECT * FROM courses WHERE id = $course_id");
$course_number = mysql_result($get_course_data,0,'course_number');
$course_name = mysql_result($get_course_data,0,'title');

$get_section_data = mysql_query("SELECT * FROM course_sections WHERE id = $section_id");
$section_number = mysql_result($get_section_data,0,'section_number');

$get_rank = mysql_query("SELECT * FROM course_section_mapping WHERE id = $cl_csm_id");
$rank = mysql_result($get_rank,0,'scheduling_rank');

$formatted_section_number = sprintf("%03d", $section_number);

echo "<h1>Delete Crosslisting for $abbreviation $course_number - $formatted_section_number</h1>";

$get_num = mysql_query("SELECT * 
						FROM course_section_mapping 
						WHERE course_section_id=$section_id 
						ORDER BY scheduling_rank");
$num_rows=mysql_numrows($get_num);

$i = 0;

while ($i < $num_rows)
{
	$rank2=mysql_result($get_num,$i,"scheduling_rank");
	$id2=mysql_result($get_num,$i,"id");
		
	if ($rank2 > $rank)
	{
		$rank2--;
		
		$result=mysql_query("UPDATE course_section_mapping
			SET scheduling_rank='$rank2' WHERE id='$id2'");
	}
	
	$i++;
}

$query = "DELETE FROM course_section_mapping WHERE id='$cl_csm_id'";
$result = mysql_query($query);

if ($result)
{
	echo "<p>";
	echo "Crosslisting for <b>$abbreviation $course_number - $formatted_section_number</b> has been successfully deleted!";
	echo "<p>";
}
else
{
	echo "<p>";
	echo "Crosslisting for <b>$abbreviation $course_number - $formatted_section_number</b> was NOT able to be deleted at this time!";
	echo "<p>";
}

//close
mysql_close();

echo "<p>";
	echo "<a href='index.php?sid=$subject_id&csm_id=$csm_id&cid=$course_id&sec_id=$section_id'>&lt;&lt; Back to Section Crosslisting Portal</a><br />";
echo "</p>";

?>

<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/bottom.inc.php');
?>